﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GoodTrader
{
    public partial class frmMDIMain : Form
    {
#region Frm Collection
        GTFrm.frmSystemChart mSystemChart = null;
        GTDownload.CpDownLoadManager mDownManager = new GTDownload.CpDownLoadManager();
        GTDownload.CpDataSet gDataSet = new GTDownload.CpDataSet();
        GTDownload.CpOptionManager mOpManager = null;
        List<GTDownload.CpOptionDownloader> mOpDownloaderList = new List<GTDownload.CpOptionDownloader>();
#endregion

        private void InitDownload()
        {
            mOpManager = new GTDownload.CpOptionManager();

            // 등가를 내려 받는다. 
            mOpManager.DownloadATM();
            // 근월물 목록을 만든다.
            mOpManager.MakeRecentMonthPriceList(1);
            // 옵션 데이터 배열을 만든다.
            for (int i = 0; i < mOpManager.RMPriceList.Count; i++)
            {
                GTDownload.CpOptionData opData = new GTDownload.CpOptionData();
                gDataSet.OptionDataList.Add(opData);
            }

            // 옵션 다운로더 리스트를 만든다.
            for (int i = 0; i < gDataSet.OptionDataList.Count; i++)
            {
                GTDownload.CpOptionDownloader opDownloader = new GTDownload.CpOptionDownloader(mDownManager, gDataSet.OptionDataList[i]);
                string code = mOpManager.RMPriceList[i];
                // 옵션 코드값을 넣어준다.
                opDownloader.Code = code;
                mDownManager.Add(opDownloader);
                mOpDownloaderList.Add(opDownloader);
            }

            GTDownload.CpFutureDownloader FutObj = new GTDownload.CpFutureDownloader(mDownManager, gDataSet.FutureData);
            FutObj.StartDate = GTGlobal.DateToInt();
            FutObj.EndDate = GTGlobal.DateToInt();
            mDownManager.Add(FutObj);
            GTDownload.CpFutureMstDownloader FutMstObj = new GTDownload.CpFutureMstDownloader(mDownManager, gDataSet.FutureMstData);
            mDownManager.Add(FutMstObj);

            GTDownload.CpSvrNew7221Downloader SubjectObj = new GTDownload.CpSvrNew7221Downloader(mDownManager, gDataSet.Svr7221Data);
            mDownManager.Add(SubjectObj);



            // 메인 데이터를 받는다.
            FutObj.BlockDownload();

            // 옵션 데이터를 먼저 받는다.
            for (int i = 0; i < mOpDownloaderList.Count; i++)
            {
                GTDownload.CpOptionDownloader opDownloader = mOpDownloaderList[i];
                opDownloader.BlockDownload();
            }

            // 실시간 데이터를 생성해 준다.
            GTDownload.CpSvr8111SDownloader ProgObj = new GTDownload.CpSvr8111SDownloader(mDownManager, gDataSet.Svr8111SData);

            mDownManager.CreateThreadFunc();
            mDownManager.Event.Set();
        }

        public frmMDIMain()
        {
            InitializeComponent();
            InitDownload();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ;
        }

        private void systemChartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ;
        }

        private void frmMDIMain_Load(object sender, EventArgs e)
        {
            mSystemChart = new GTFrm.frmSystemChart();
            mSystemChart.CpDataSetObj = gDataSet;
            mSystemChart.MdiParent = this;
            mSystemChart.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            mSystemChart.Show();
        }

        private void frmMDIMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            mDownManager.DownNum = -1;
            mDownManager.DownThreadObject.Abort();
           
        }

        private void codeTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GTOption.CpOptionCodeTable opCodeTable = new GTOption.CpOptionCodeTable(mOpManager);
            opCodeTable.Show();
        }

        private void optionChartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GTChart.frmOptChart optChart = new GTChart.frmOptChart();
            optChart.CpDataSetObj = gDataSet;
            optChart.MdiParent = this;
            optChart.Show();
        }
    }
}
